<?php

// phpMyRealty 3
//
// File Name: mail.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// Include configuration file and general functions
define('PMR', 'true');

$page = 'contact';

include ( './config.php' );
include ( PATH . '/defaults.php' );

generateCaptcha();
 
$rand = mt_rand(1, 999);

// ----------------------------------------------------------------------
// SEND MESSAGE SECTION

// Title tag content
$title = $conf['website_name_short'] . ' - ' . $lang['Mailer'];

//Template header
include ( PATH . '/templates/' . $cookie_template . '/header.php' );

// We make sure that the id variable is correct

 {

  // If the Submit button was pressed we start this routine
  if (isset($_POST['submit_mailer']) 
  && $_POST['submit_mailer'] == $lang['Mailer_Submit'])
   {

    $form = array();

    // safehtml() all the POST variables
    // to insert into the database or
    // print the form again if errors
    // found
    $form = array_map('safehtml', $_POST);

    echo table_header ( $lang['Information'] );

    // Initially we think that no errors were found
    $count_error = 0;

    if ( strtoupper($_POST['security']) != $_SESSION['random'] )
     { echo $lang['Field_Empty'] . ' - <span class="warning">' . $lang['Security_Code'] . ' </span><br />'; $count_error++;}

    // Check for the empty or incorrect required fields
    if (empty($form['e_mail']) || strlen($form['e_mail']) < 4  || !valid_email($form['e_mail']))
     { echo $lang['Field_Empty'] . ' - <span class="warning">' . $lang['Mailer_e_mail'] . '</span><br />'; $count_error++;}

    if ( (time() - $session->fetch('mail_time')) < 30 )
     { echo $lang['Flood_Detected'] . '<br />'; $count_error++;}

    if (empty($form['message']) || strlen($form['message']) < 4 )
     { echo $lang['Field_Empty'] . ' - <span class="warning">' . $lang['Mailer_Message'] . '</span><br />'; $count_error++;}

    if ($count_error > '0')
     echo '<br /><span class="warning">' . $lang['Errors_Found'] . ': ' . $count_error . '</span><br />';

    // If no errors were found during the above checks we continue
    if ($count_error == '0')
     {

      if ( strtoupper($_POST['security']) == $_SESSION['random'] ) $session->varunset('random');

      $f = $db->fetcharray($r);

//

      $mail = new PHPMailer();

      if(PHPMAILER == '3') {
       $mail->IsSMTP(); // set mailer to use SMTP
       $mail->Host = $smtp['host'];  // specify main and backup server
       $mail->SMTPAuth = true;     // turn on SMTP authentication
       $mail->Username = $smtp['login'];  // SMTP username
       $mail->Password = $smtp['password']; // SMTP password
      }
      elseif(PHPMAILER == '2') {
       $mail->IsSendmail(); // set mailer to use SMTP
      }
      else {
      }

      $mail->From = $conf['general_e_mail'];
      $mail->FromName = $conf['general_e_mail_name'];
      $mail->AddAddress($conf['general_e_mail']);

      // Replace some variables in the subject
      $lang['Mailer_Subject'] = str_replace('{website_name}', $conf['website_name'], $lang['Mailer_Subject']);
      $lang['Mailer_Subject'] = str_replace('{user_e_mail}', $form['e_mail'] , $lang['Mailer_Subject']);

      $mail->Subject = $lang['Mailer_Subject'];

      $mail_message = $form['message'] . ' Name: ' . $form['name'] . ', Address: ' . $form['address'] . ', Phone: ' . $form['phone'];

      $mail->MsgHTML    = $mail_message;
      $mail->AltBody = removehtml($mail_message);
 
      $mail->Send();

      // Show the Thank you message
      $lang['Mailer_Sent'] = str_replace('{name}', $conf['website_name'], $lang['Mailer_Sent']);

      echo $lang['Mailer_Sent'];

      // Save last message post timestamp not to allow
      // this user post again in 30 seconds
      $session->set('mail_time', time());

     }

    echo table_footer ( );

   }

  // If we open sendmessage.php for the first time
  // or there were errors found in the form fields 
  // we output the form again with the old variables 
  // included
  if (!isset($count_error) || $count_error > '0')
   {

    echo table_header ( $lang['Mailer'] );

    // Define the form variables if the form is loaded for the first time
    if (!isset($form))
     {
      $form = array();
      $form['e_mail'] = '';
      $form['message'] = '';

      $form['name'] = '';
      $form['address'] = '';
      $form['phone'] = '';
     }

    // Output the form
    echo '
     <form action="' . URL . '/mail.php" method="POST">
      <table width="100%" cellpadding="5" cellspacing="0" border="0">
         ';

    echo userform ($lang['Realtor_First_Name'] . ', ' . $lang['Realtor_Last_Name'], '<input type="text" size="45" name="name" value="' . $form['name'] . '" maxlength="100">', '1');
    echo userform ($lang['Realtor_Address'], '<input type="text" size="45" name="address" value="' . $form['address'] . '" maxlength="255">', '1');
    echo userform ($lang['Realtor_Phone'], '<input type="text" size="45" name="phone" value="' . $form['phone'] . '" maxlength="50">', '1');
    echo userform ($lang['Mailer_e_mail'], '<input type="text" size="45" name="e_mail" value="' . $form['e_mail'] . '" maxlength="255">', '1');
    echo userform ($lang['Mailer_Message'], '<textarea cols="45" rows="4"  name="message" onKeyDown="textCounter(this.form.message,this.form.message_counter, ' . $conf['mailer_message_size'] . ');" onKeyUp="textCounter(this.form.message,this.form.message_counter, ' . $conf['mailer_message_size'] . ');">' . unsafehtml($form['message']) . '</textarea>', '1');
    echo userform ('', '<input readonly type="text" name="message_counter" size="5" maxlength="5" value="' . $conf['mailer_message_size'] . '"> ' . $lang['Characters_Left']);

    echo userform ($lang['Security_Code'], '<img src="' . URL . '/security.php?' . $rand . '" border="0" alt=""><input type="text" size="25" name="security" value="" maxlength="255">' , '1');
 
    echo userform ('', '<input type="Submit" class="submit" name="submit_mailer" value="' . $lang['Mailer_Submit'] . '">');

    echo '
      </table>
     </form>
    ';

    echo table_footer ();
   }

}

// Template footer
include ( PATH . '/templates/' . $cookie_template . '/footer.php' );

?>